﻿using System.IO.Ports;
using Digitalmes.Core.Common.SerialPorts;

namespace Digitalmes.Infrastructure.IO.SerialPorts;

/// <summary>
/// RS232 串口对象
/// </summary>
/// <param name="serialPort">
/// SerialPort类串口实例对象,提供串口的读写等操作
/// </param>
internal sealed class RsSerialPort(SerialPort serialPort) : IRsSerialPort, IDisposable
{
    public string? Purpose { get; init; }

    public string PortName => serialPort.PortName;

    public bool IsOpen => serialPort.IsOpen;

    public void Open()
    {
        if (!serialPort.IsOpen)
        {
            serialPort.Open();
        }
    }

    public void Close()
    {
        if (serialPort.IsOpen)
        {
            serialPort.Close();
        }
    }

    public void Dispose()
    {
        serialPort.Dispose();
    }
}
